﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：管理块信息
//********************************************************************************//
using System;
using System.Linq;
using Web.Class;
using System.Data;
using Entity;
using BLL;
using BLL.Access;

namespace Web.Pages.YongShui.JiChuXX
{
    public partial class GuanLiKXX : BasePage
    {
        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int GuanLiKuaiID
        {
            get
            {
                return Convert.ToInt32(ViewState["_GuanLiKuaiID"]);
            }
            set
            {
                ViewState["_GuanLiKuaiID"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 管理块对象
        /// </summary>
        YS_JX_GUANLIK _YS_JX_GUANLIK = new YS_JX_GUANLIK();

        /// <summary>
        /// 管理块BLL
        /// </summary>
        YS_JX_GUANLIKBLL _YS_JX_GUANLIKBLL = new YS_JX_GUANLIKBLL();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                LoadData();
                Display();
                FocusChange();
            }
            LoadData();
            //将当前页面对像传递给确认控件
           
        }

        /// <summary>
        /// 站点编号选择索引改变事件
        /// </summary>
        protected void cmbZhanDianBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbZhanDianMC.SelectedIndex = ((DevExpress.Web.ASPxComboBox)(sender)).SelectedIndex;
        }

        /// <summary>
        /// 树聚焦行改变事件
        /// </summary>
        protected void treeGuanLiK_FocusedNodeChanged(object sender, EventArgs e)
        {
            FocusChange();
        }

        /// <summary>
        /// 添加操作
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Add;
            Display();
        }

        /// <summary>
        /// 修改操作
        /// </summary>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Update;
            Display();
        }

        /// <summary>
        /// 放弃操作
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;
            FocusChange();
            Display();
        }

        /// <summary>
        /// 保存操作
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                SaveData();
            }
        }
        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (this.txtGuanLiKH.Text == "")
            {
                PageTools.AlertMsg(this, "请输入管理块号！");
                this.txtGuanLiKH.Focus();
                return false;
            }
            if (!PageTools.ValidateString(this.txtGuanLiKH.Text,PageTools.eValidateType.数字))
            {
                PageTools.AlertMsg(this, "你输入的格式不正确，请输入数字类型。");
                this.txtGuanLiKH.Focus();
                return false;
            }
            if (this.txtGuanLiKMC.Text == "")
            {
                PageTools.AlertMsg(this, "请输入管理块名称！");
                this.txtGuanLiKMC.Focus();
                return false;
            }
            return true;
        }
        /// <summary>
        /// 删除操作
        /// </summary>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (null != treeGuanLiK.FocusedNode)
            {
                var node = treeGuanLiK.FocusedNode;
                if (node.Level == 1)
                {
                    return;
                }
            }
            //确认是否有用户使用
            _YS_JX_GUANLIK = this._YS_JX_GUANLIKBLL.SelectYS_JX_GUANLIK(this.GuanLiKuaiID, null);
            if (new YS_YG_YONGHUXXBLL().SelectYS_YG_YONGHUXXesDynamic(string.Format("s_guanlikbh='{0}'", _YS_JX_GUANLIK.S_GUANLIKH), null).Rows.Count > 0)
            {
                PageTools.AlertMsg(this, "该管理块尚有正在使用的用户,不能进行删除操作。");
                return;
            }
            PageTools.ConfirmMsg(this, "要删除该管理块信息吗？");
        }

        /// <summary>
        /// 执行删除
        /// </summary>
        protected void btnDelFact_Click(object sender, EventArgs e)
        {
            //判断用户是否确认
            if (Convert.ToBoolean(this.confirmValue.Value))
            {
                _YS_JX_GUANLIK = this._YS_JX_GUANLIKBLL.SelectYS_JX_GUANLIK(this.GuanLiKuaiID);
                this._YS_JX_GUANLIK.S_CAOZUOR = LoginInfo.CurrentUser.Account;
                this._YS_JX_GUANLIK.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                this._YS_JX_GUANLIK.I_JLZT = -1;
                _SYS_LOGBLL.Log<YS_JX_GUANLIK>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "管理块信息", OperateState.Delete, _YS_JX_GUANLIK, null);
                if (_YS_JX_GUANLIKBLL.InsertOrUpdate(_YS_JX_GUANLIK, null))
                {
                    
                    Display();
                    LoadData();
                    this.Status = GridViewStatus.Init;
                    this.treeGuanLiK.GetAllNodes().First().Focus();
                    FocusChange();
                    PageTools.AlertMsg(this, "操作成功！");
                }
            }
        }


        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //绑定下拉框
            WordsManager manger = new WordsManager();
            PageTools.BindComboBox(cmbQuKuaiLX, manger.Instance.FindWordsDataTableWithMiddleAffixByBelongID(WordsStruct.BelongID.区块类型, " "));
            var ds = _spHelper.P_YS_GUANLIKDROPDOWNLIST();
            if (null != ds && ds.Tables.Count > 0)
            {
                //移除站点"中心"
                int index = -1;
                for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
                {
                    if (Convert.ToString(ds.Tables[0].Rows[i][0]) == "00")
                    {
                        index = i;
                        break;
                    }
                }
                if(index!=-1)
                {
                    ds.Tables[0].Rows.RemoveAt(index);
                    ds.Tables[1].Rows.RemoveAt(index);
                }
                foreach (DataRow row in ds.Tables[1].Rows)
                {
                    if (Convert.ToString(row[0]) == "中心")
                    {
                        ds.Tables[1].Rows.Remove(row);
                        break;
                    }
                }
                PageTools.BindComboBox(cmbZhanDianBH, ds.Tables[0]);
                PageTools.BindComboBox(cmbZhanDianMC, ds.Tables[1]);
            }
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.txtGuanLiKH.Text = "";
            this.txtGuanLiKMC.Text = "";
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            var ds = _spHelper.P_YS_GUANLIKQUERY();
            if (null != ds && ds.Tables.Count > 0)
            {
                var table = ds.Tables[0];                
                treeGuanLiK.KeyFieldName = "BIANHAO";
                treeGuanLiK.ParentFieldName = "BELONGID";
                treeGuanLiK.DataSource = table;
                treeGuanLiK.DataBind();
            }
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    this.btnAdd.Visible = true;
                    this.btnEdit.Visible = true;
                    this.btnSave.Visible = false;
                    this.btnCancel.Visible = false;
                    this.btnDel.Visible = true;
                    this.treeGuanLiK.Enabled = true;
                    SetText(false);
                    break;
                case GridViewStatus.Search:
                    SetText(false);
                    break;
                case GridViewStatus.Update:
                    this.btnAdd.Visible = false;
                    this.btnEdit.Visible = false;
                    this.btnSave.Visible = true;
                    this.btnCancel.Visible = true;
                    this.btnDel.Visible = false;
                    this.treeGuanLiK.Enabled = false;
                    SetText(true);
                    this.txtGuanLiKH.Enabled = false;
                    this.txtGuanLiKMC.Focus();
                    break;
                case GridViewStatus.Add:
                    this.btnAdd.Visible = false;
                    this.btnEdit.Visible = false;
                    this.btnSave.Visible = true;
                    this.btnCancel.Visible = true;
                    this.btnDel.Visible = false;
                    this.treeGuanLiK.Enabled = false;
                    SetText(true);
                    InitData();
                    this.txtGuanLiKH.Focus();
                    break;
            }
        }

        /// <summary>
        /// 设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
            this.txtGuanLiKH.Enabled = b;
            this.txtGuanLiKMC.Enabled = b;
            this.cmbQuKuaiLX.Enabled = b;
            this.cmbZhanDianBH.Enabled = b;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //判断是否选择了站点
            if(Convert.ToInt32(this.cmbZhanDianBH.Value)<=0)
            {
                PageTools.AlertMsg(this,"请选择站点！");
                return;
            }
            //添加
            if (this.Status == GridViewStatus.Add)
            {
                operateState = OperateState.Add;
                this._YS_JX_GUANLIK.ID = 0;
                this._YS_JX_GUANLIK.I_JLZT = 0;
            }
            //修改
            else
            {
                //修改操作
                operateState = OperateState.Modify;
                this._YS_JX_GUANLIK = this._YS_JX_GUANLIKBLL.SelectYS_JX_GUANLIK(this.GuanLiKuaiID);
            }
            //判断是否编号已存在
            if (_YS_JX_GUANLIKBLL.SelectYS_JX_GUANLIKsDynamic(string.Format("I_JLZT=0 and S_GUANLIKH='{0}' and ID<>'{1}'", this.txtGuanLiKH.Text, this._YS_JX_GUANLIK.ID), null).Rows.Count > 0)
            {
                PageTools.AlertMsg(this, "该管理块号已经使用,请重新输入管理块号信息。");
                this.txtGuanLiKH.Focus();
                return;
            }           
            this._YS_JX_GUANLIK.S_GUANLIKH = this.txtGuanLiKH.Text;
            this._YS_JX_GUANLIK.S_GUANLIKMC = this.txtGuanLiKMC.Text;
            this._YS_JX_GUANLIK.S_ZHANDIAN = this.cmbZhanDianBH.Value.ToString();
            this._YS_JX_GUANLIK.I_LEIXING = Convert.ToDecimal(this.cmbQuKuaiLX.Value);
            this._YS_JX_GUANLIK.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            this._YS_JX_GUANLIK.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            //执行操作
            _SYS_LOGBLL.Log<YS_JX_GUANLIK>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "管理块信息", operateState, _YS_JX_GUANLIK, null);
            if (this._YS_JX_GUANLIKBLL.InsertOrUpdate(_YS_JX_GUANLIK, null))
            {
                                
                PageTools.AlertMsg(this,"保存成功！");
                LoadData();
                this.Status = GridViewStatus.Init;
                Display();
                //设置选中当前操作的项
                this.treeGuanLiK.FindNodeByKeyValue(this._YS_JX_GUANLIK.S_GUANLIKH).Focus();
                //更新当前操作项ID
                this.GuanLiKuaiID = Convert.ToInt32(((System.Data.DataRowView)(treeGuanLiK.FocusedNode.DataItem)).Row.ItemArray[0].ToString().Split('_')[0]);
            }
            else
            {
                PageTools.AlertMsg(this,"保存失败！");
            }
        }

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            txtGuanLiKH.Text = _YS_JX_GUANLIK.S_GUANLIKH;
            txtGuanLiKMC.Text = _YS_JX_GUANLIK.S_GUANLIKMC;
            cmbZhanDianBH.Value = _YS_JX_GUANLIK.S_ZHANDIAN;
            cmbZhanDianMC.Value = _YS_JX_GUANLIK.S_ZHANDIAN;
            cmbQuKuaiLX.Value = _YS_JX_GUANLIK.I_LEIXING.ToString();
        }

        /// <summary>
        /// 左侧列表树焦点变更方法
        /// </summary>
        private void FocusChange()
        {
            if (null != treeGuanLiK.FocusedNode)
            {
                var node = treeGuanLiK.FocusedNode;
                if (node.Level == 2)
                {
                    this.GuanLiKuaiID = Convert.ToInt32(((System.Data.DataRowView)(node.DataItem)).Row.ItemArray[0].ToString().Split('_')[0]);
                    _YS_JX_GUANLIK = _YS_JX_GUANLIKBLL.SelectYS_JX_GUANLIK(this.GuanLiKuaiID);
                    SetData();
                }
                else
                {
                    if (node.ChildNodes.Count > 0)
                    {
                        var childNode = node.ChildNodes[0];
                        this.GuanLiKuaiID = Convert.ToInt32(((System.Data.DataRowView)(childNode.DataItem)).Row.ItemArray[0].ToString().Split('_')[0]);
                        _YS_JX_GUANLIK = _YS_JX_GUANLIKBLL.SelectYS_JX_GUANLIK(this.GuanLiKuaiID);
                        SetData();
                    }
                }
            }
        }

        #endregion       
    }
}